package com.mtnz.njk.service;

import com.github.pagehelper.PageHelper;
import com.mtnz.njk.Mapper.ManureMapper;
import com.mtnz.njk.Mapper.PesticideMapper;
import com.mtnz.njk.entity.ManureEntity;
import com.mtnz.njk.entity.PesticideEntity;
import com.mtnz.njk.result.PageInfo;
import com.mtnz.njk.result.ResponseMessage;
import com.mtnz.njk.result.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Description: 查询农药业务层
 * @Author: FBL
 * @CreateDate: 2019\7\31 0031 1:06
 * @Version: 1.0
 */
@Service
public class QueryPesticideService {

    @Autowired
    PesticideMapper pesticideMapper;

    @Autowired
    ManureMapper manureMapper;

    /**
     * 查询农药
     *
     * @param registeredNumber
     * @param companyName
     * @param effective
     * @return
     */
    public ResponseResult queryPesticide(String registeredNumber, String companyName, String effective, String pesticideId, Integer pageNum, Integer pageSize)  throws Exception{
        PageHelper.startPage(pageNum, pageSize);
        // 查询农药详情
        if (pesticideId != null && !pesticideId.equals("")) {
            PesticideEntity pesticideEntity = pesticideMapper.selectByPrimaryKey(pesticideId);
            return new ResponseResult(ResponseMessage.OK, pesticideEntity);
        }

        if (registeredNumber == null && companyName == null && effective == null) {
            List<PesticideEntity> pesticideEntities = pesticideMapper.selectAll();
            PageInfo<PesticideEntity> pesticideEntityPageInfo = new PageInfo<>(pesticideEntities);
            return new ResponseResult(ResponseMessage.OK, pesticideEntityPageInfo);
        }
        // 关键字模糊匹配
        Example pesticideExample = new Example(PesticideEntity.class);
        Example.Criteria criteria = pesticideExample.createCriteria();
        if (registeredNumber != null && !registeredNumber.equals("")) {
            criteria.andLike("registeredNumber", "%" + registeredNumber + "%");
        }
        if (companyName != null && !companyName.equals("")) {
            criteria.andLike("companyName", "%" + companyName + "%");
        }
        if (effective != null && !effective.equals("")) {
            criteria.andLike("effective", "%" + effective + "%");
        }
        List<PesticideEntity> pesticideEntities = pesticideMapper.selectByExample(pesticideExample);
        PageInfo<PesticideEntity> pesticideEntityPageInfo = new PageInfo<>(pesticideEntities);
        return new ResponseResult(ResponseMessage.OK, pesticideEntityPageInfo);
    }

    /**
     * 查询肥料
     *
     * @param registeredNumber
     * @param companyName
     * @param effective
     * @return
     */
    public ResponseResult queryManure(String registeredNumber, String companyName, String effective, String manureId, Integer pageNum, Integer pageSize)  throws Exception{
        PageHelper.startPage(pageNum, pageSize);
        // 查询肥料详情
        if (manureId != null && !manureId.equals("")) {
            ManureEntity manureEntity = manureMapper.selectByPrimaryKey(manureId);
            return new ResponseResult(ResponseMessage.OK, manureEntity);
        }

        if (registeredNumber == null && companyName == null && effective == null) {
            List<ManureEntity> manureEntities = manureMapper.selectAll();
            PageInfo<ManureEntity> manureEntityPageInfo = new PageInfo<>(manureEntities);
            return new ResponseResult(ResponseMessage.OK, manureEntityPageInfo);
        }
        // 关键字模糊匹配
        Example pesticideExample = new Example(PesticideEntity.class);
        Example.Criteria criteria = pesticideExample.createCriteria();
        if (registeredNumber != null && !registeredNumber.equals("")) {
            criteria.andLike("registeredNumber", "%" + registeredNumber + "%");
        }
        if (companyName != null && !companyName.equals("")) {
            criteria.andLike("companyName", "%" + companyName + "%");
        }
        if (effective != null && !effective.equals("")) {
            criteria.andLike("effective", "%" + effective + "%");
        }
        List<ManureEntity> manureEntities = manureMapper.selectByExample(pesticideExample);
        PageInfo<ManureEntity> manureEntityPageInfo = new PageInfo<>(manureEntities);
        return new ResponseResult(ResponseMessage.OK, manureEntityPageInfo);
    }
}
